package nm04;

import nm03.testdemo.Ye;
import nm05完成版角A角3.UpHandPai;

import java.lang.reflect.AccessibleObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Fu {
    public static int HuiHeShuxunhuan = 0;//
    public static int HuiHeShu = HuiHeShuxunhuan / 4;//显示回合数
    public static ArrayList<String> UpShouPai = new ArrayList<>();//上一手牌
    public static ArrayList<String> NowShouPai = new ArrayList<>();//现一手牌
    public static int UpYiShouPaiXing = 0;//1 单 2 双 3 三只 4 四条 5 顺子 6 同花 7 3+2 8 4+1 9 同花顺
    public static int NowShouPaiXing = 0;//1 单 2 双 3 三只 4 四条 5 顺子 6 同花 7 3+2 8 4+1 9 同花顺

    public static int pass = 0;//计算pass
    public static int shu = 52;
    public static ArrayList<String> Pai = new ArrayList<>();//存储所有牌的样式 例如:13 3 ♦ 4 // 13为给定的数（自定义牌号大小）， 3是牌号 ，♦是花色, 4 是花色大小。
    public static ArrayList<String> showPlay = new ArrayList<>();//展示玩家手牌
    public static ArrayList<String> Play = new ArrayList<>();//存储玩家手牌信息
    public static ArrayList<String> beifen = new ArrayList<>();

    public static ArrayList<String> AI1 = new ArrayList<>();
    public static ArrayList<String> AI2 = new ArrayList<>();
    public static ArrayList<String> AI3 = new ArrayList<>();//手牌
    public static String[] play = new String[13];//数组存储以便将来更改了集合，要查找原来手牌，记录作用可以删。占时没用上
    public static String[] ai1 = new String[13];
    public static String[] ai2 = new String[13];
    public static String[] ai3 = new String[13];
    public int playMH4 = 0;
    public int playJA = 0;
    public int playJ3 = 0;//记录角A角3 梅花4的人,以便判断胜利方
    public int ai1MH4 = 0;
    public int ai1JA = 0;
    public int ai1J3 = 0;
    public int ai2MH4 = 0;
    public int ai2JA = 0;
    public int ai2J3 = 0;
    public int ai3MH4 = 0;
    public int ai3JA = 0;
    public int ai3J3 = 0;
    public static String meihua = "01 4 ♣ 1 11";//方便找梅花4
    public static String jiaoA = "11 A ♦ 4 54";//方便找角A
    public static String jiao3 = "13 3 ♦ 4 62";//方便找角3

    public static int PMzhuP = 0;//平民1
    public static int PMzhuAI1 = 0;//平民2
    public static int PMzhuAI2 = 0;//平民3
    public static int PMzhuAI3 = 0;//平民4
    public static int DJzhuP = 0;//地主
    public static int DJzhuAI1 = 0;//地主
    public static int DJzhuAI2 = 0;//地主
    public static int DJzhuAI3 = 0;//地主



    public static int[] ai1PaiXing = new int[9];//对应的九个位置是
    public static int[] ai2PaiXing = new int[9];//0 单张 1. 对子 2. 斋三条 3. 斋四条 4. 3+2 5. 4+1 6.顺子 7.同花 8.同花顺
    public static int[] ai3PaiXing = new int[9];
//======================================================================================================================


    static {

        String[] Hua = {" ♣", " ♥", " ♠", " ♦"};
        String[] Dian = {" 4", " 5", " 6", " 7", " 8", " 9", "10", " J", " Q", " K", " A", " 2", " 3"};
        int shu = 11;
       /* for (int i = 0; i < Hua.length; i++) {
            for (int j = 0; j < Dian.length; j++) {
                if (j < 9) {
                    Pai.add("0" + (j + 1) + Dian[j] + Hua[i] + " " + (i + 1));
                } else {
                    Pai.add((j + 1) + Dian[j] + Hua[i] + " " + (i + 1));
                }

            }
        }*/
        for (int i = 0; i < Dian.length; i++) {

            for (int j = 0; j < Hua.length; j++) {
                if(i<9) {
                    Pai.add("0" + (i + 1) + Dian[i] + Hua[j] + " " + (j + 1) +" " +shu);
                    shu++;
                }else {
                    Pai.add((i + 1) + Dian[i] + Hua[j] + " " + (j + 1) +" " + shu);
                    shu++;
                }
            }
        }

    }

    //发牌=========================================================================


    public void PaiXu(ArrayList<String> who) {
        int[] arr = new int[who.size()];//存储给定的数
        int[] arr1 = new int[who.size()];//存花色号
        ArrayList<String> jiu = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = Integer.parseInt(who.get(i).charAt(9) + "" + who.get(i).charAt(10));
            arr1[i] = Integer.parseInt(who.get(i).charAt(7)+"");
        }
        for (int i = 0; i < arr1.length; i++) {
            System.out.print(arr1[i]+" ");
        }
        System.out.println("排序成功" + who);
        System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");

        for (int i = 0; i < who.size(); i++) {
            for (int j = 0; j < who.size(); j++) {
                if (arr[i] < arr[j]) {
                    int sb = arr[i];
                    arr[i] = arr[j];
                    arr[j] = sb;
                    int sb1 = arr1[i];
                    arr1[i] = arr1[j];
                    arr1[j] = sb1;
                }
            }
        }

       /* System.out.println("+++++++++");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }*/
        for (int i = 0; i < arr.length; i++) {

            for (int j = 0; j < arr.length; j++) {
                if (arr[i] == Integer.parseInt(who.get(j).charAt(9) + "" + who.get(j).charAt(10)) && arr1[i] == Integer.parseInt(who.get(j).charAt(7)+"")) {
                    jiu.add(who.get(j));
                }
            }
        }

        who.clear();
        for (int i = 0; i < jiu.size(); i++) {
            who.add(i, jiu.get(i));
        }
//        System.out.println(who);
    }

    public void ShowPlayPai(ArrayList<String> who) {
        showPlay.clear();
        for (int i = 0; i < who.size(); i++) {
            showPlay.add(who.get(i).charAt(5) + "" + who.get(i).charAt(2) + who.get(i).charAt(3));
        }
        System.out.println("您现在的手牌如下,底下对应的是你待会要出牌的操作数");
        System.out.println(showPlay);
        for (int i = 0; i < who.size(); i++) {
            if (i < 9) {
                System.out.print("   " + (i + 1) + " ");
            } else {
                System.out.print("  " + (i + 1) + " ");
            }

        }
    }//展示玩家当前手牌

    public void ShowUpYiShouPai() {
        ArrayList<String> show = new ArrayList<>();
        for (int i = 0; i < UpShouPai.size(); i++) {
            show.add(UpShouPai.get(i).charAt(5) + "" + UpShouPai.get(i).charAt(2) + UpShouPai.get(i).charAt(3));
        }
        System.out.println("上一手是:" + show);
        switch (UpShouPai.size()) {
            case 1:
                System.out.println("上一手是单张牌型");
                System.out.println("您只能出单张");
                System.out.println("请输入 1 进入选牌阶段");
                break;
            case 2:
                System.out.println("上一手是对子牌型");
                System.out.println("您只能出对子牌型");
                System.out.println("请输入 2 进入选牌阶段");
                break;
            case 3:
                System.out.println("上一手是三条脱裤子牌型");
                System.out.println("您只能出三条脱裤子牌型");
                System.out.println("请输入 3 进入选牌阶段");
                break;
            case 4:
                System.out.println("上一手是四条脱裤子牌型");
                System.out.println("您只能出四条脱裤子牌型");
                System.out.println("请输入 4 进入选牌阶段");
                break;
            case 5:
                System.out.println("上一手是道子牌型");
                System.out.println("您只能出道子牌型");
                System.out.println("请输入 5 进入选牌阶段");
                break;

        }

    }//展示上一手牌

    public void getPlaypai() {
        FaPai(Play, play);
    }

    public void getAi1() {
        FaPai(AI1, ai1);
    }

    public void getAi2() {
        FaPai(AI2, ai2);
    }

    public void getAi3() {
        FaPai(AI3, ai3);
    }


    private static void FaPai(ArrayList<String> p, String[] s) {
        Random r = new Random();
        for (int i = 0; i < 13; i++) {
            int rand = r.nextInt(shu);
            s[i] = Pai.get(rand);
            p.add(Pai.get(rand));
            Pai.remove(rand);
//            int a = Integer.parseInt(p.get(0).charAt(0)+"");
            shu--;
        }
    }
    //===================================================================================

    //开始=========================================================================
    public void ChaZhaoMH4() {


        if (Play.contains(meihua)) {
            playMH4++;
        } else if (AI1.contains(meihua)) {
            ai1MH4++;
        } else if (AI2.contains(meihua)) {
            ai2MH4++;
        } else {
            ai3MH4++;
        }

    }

    public void ChaZhaoJAJ3() {


        if (Play.contains(jiao3)) {
            playJ3++;
        } else if (AI1.contains(jiao3)) {
            ai1J3++;
        } else if (AI2.contains(jiao3)) {
            ai2J3++;
        } else {
            ai3J3++;
        }


        if (Play.contains(jiaoA)) {
            playJA++;
        } else if (AI1.contains(jiaoA)) {
            ai1JA++;
        } else if (AI2.contains(jiaoA)) {
            ai2JA++;
        } else {
            ai3JA++;
        }
    }

    public void KaiShi() {

        //发牌
        getPlaypai();//给玩家发牌
        getAi1();
        getAi2();
        getAi3();
        ChaZhaoMH4();//查找梅花四的位置
        ChaZhaoJAJ3();//查找角A角B的位置
        PaiXu(Play);
        PaiXu(AI1);
        PaiXu(AI2);
        PaiXu(AI3);
        System.out.println("玩家牌："+Play);
        System.out.println("AI1牌："+AI1);
        System.out.println("AI2牌："+AI2);
        System.out.println("AI3牌："+AI3);
        //发牌

        //循环开始

        if (playMH4 == 1) {
            WanJia();
        } else if (ai1MH4 == 1) {
            AI1c();
        } else if (ai2MH4 == 1) {
            AI2c();
        } else {
            AI3c();
        }


    }

    //=====================================================================================

    //胜利条件=====================================================================
    public void PanDuanPTShengChu() {
        if(PMzhuAI1>0 && PMzhuAI2>0){
            System.out.println("AI1和AI2平民胜利");
            System.exit(1);
        }else if(PMzhuAI1>0 && PMzhuAI3>0){
            System.out.println("AI1和AI3平民胜利");
            System.exit(1);
        }else if(PMzhuAI1>0 && PMzhuP>0){
            System.out.println("AI1和玩家平民胜利");
            System.exit(1);
        }else if(PMzhuAI2>0 && PMzhuAI3>0){
            System.out.println("AI2和AI3平民胜利");
            System.exit(1);
        }else if(PMzhuAI2>0&&PMzhuP>0){
            System.out.println("AI2和玩家平民胜利");
            System.exit(1);
        }else if(PMzhuAI3>0 && PMzhuP>0){
            System.out.println("AI3和玩家平民胜利");
            System.exit(1);
        }
    }

    public void PanDuanDJShengChu() {
        if(DJzhuAI1>0 && DJzhuAI2>0){
            System.out.println("AI1和AI地主胜利");
            System.exit(1);
        }else if(DJzhuAI1>0 && DJzhuAI3>0){
            System.out.println("AI1和AI3地主胜利");
            System.exit(1);
        }else if(DJzhuAI1>0 && DJzhuP>0){
            System.out.println("AI1和玩家地主胜利");
            System.exit(1);
        }else if(DJzhuAI2>0 && DJzhuAI3>0){
            System.out.println("AI2和AI3地主胜利");
            System.exit(1);
        }else if(DJzhuAI2>0&&DJzhuP>0){
            System.out.println("AI2和玩家地主胜利");
            System.exit(1);
        }else if(DJzhuAI3>0 && DJzhuP>0){
            System.out.println("AI3和玩家地主胜利");
            System.exit(1);
        }
    }
    //=============================================================================

    //规则========================================================================
    //对比牌型大小
    static class guize {

        //出牌规则===============================
        public static void gzDan() {

        }//占时用不上

        public static boolean gzTwo() {
            int one = Integer.parseInt(NowShouPai.get(0).charAt(0) + "" + NowShouPai.get(0).charAt(1));
            int two = Integer.parseInt(NowShouPai.get(1).charAt(0) + "" + NowShouPai.get(1).charAt(1));
            return one == two;
        }

        public static boolean gzThree() {
            int one = Integer.parseInt(NowShouPai.get(0).charAt(0) + "" + NowShouPai.get(0).charAt(1));
            int two = Integer.parseInt(NowShouPai.get(1).charAt(0) + "" + NowShouPai.get(1).charAt(1));
            int three = Integer.parseInt(NowShouPai.get(2).charAt(0) + "" + NowShouPai.get(2).charAt(1));
            return one == two && one == three;
        }


        public static boolean gzFour() {
            int one = Integer.parseInt(NowShouPai.get(0).charAt(0) + "" + NowShouPai.get(0).charAt(1));
            int two = Integer.parseInt(NowShouPai.get(1).charAt(0) + "" + NowShouPai.get(1).charAt(1));
            int three = Integer.parseInt(NowShouPai.get(2).charAt(0) + "" + NowShouPai.get(2).charAt(1));
            int four = Integer.parseInt(NowShouPai.get(3).charAt(0) + "" + NowShouPai.get(3).charAt(1));
            return one == two && one == three && one == four;
        }

        public static boolean gzFire() {
            String jieShouFire = FirePaiXu();
            StringBuilder HuaSe = new StringBuilder();
            for (int i = 0; i < 5; i++) {
                HuaSe.append(NowShouPai.get(i).toString().charAt(7));
            }
            if (Ye.shunZi.contains(jieShouFire) && Ye.tongHua.contains(HuaSe.toString())) {
                NowShouPaiXing = 9;
                return true;
            } else if (Ye.fourOne.contains(jieShouFire)) {
                NowShouPaiXing = 8;
                return true;
            } else if (Ye.threeTwo.contains(jieShouFire)) {
                NowShouPaiXing = 7;
                return true;
            } else if (Ye.shunZi.contains(jieShouFire)) {
                NowShouPaiXing = 5;
                return true;
            } else if (Ye.tongHua.contains(HuaSe.toString())) {
                NowShouPaiXing = 6;
                return true;
            } else {
                return false;
            }
        }

        public static String FirePaiXu() {
            ArrayList<String> who = NowShouPai;
            StringBuilder fireYangShi = new StringBuilder();
            int[] arr = new int[who.size()];//存储给定的数
            int[] arr1 = new int[who.size()];//存花色号
            ArrayList<String> jiu = new ArrayList<>();
            for (int i = 0; i < arr.length; i++) {
                arr[i] = Integer.parseInt(who.get(i).charAt(0)+""+who.get(i).charAt(1));
                fireYangShi.append(arr[i]);
            }
            return fireYangShi.toString();
        }
        //======================================

        //牌型比较===============================
        public static boolean danDuiBi() {
            int a = Integer.parseInt(NowShouPai.get(0).charAt(9) + "" + NowShouPai.get(0).charAt(10));
            int b = Integer.parseInt(UpShouPai.get(0).charAt(9) + "" + UpShouPai.get(0).charAt(10));
            if (a > b) {
                System.out.println("对比成功");
                pass = 0;
                return true;
            } else {
                return false;
            }
        }

        public static boolean twoDuiBi() {
            int a = Integer.parseInt(NowShouPai.get(0).charAt(0) + "" + NowShouPai.get(0).charAt(1));
            int b = Integer.parseInt(UpShouPai.get(0).charAt(0) + "" + NowShouPai.get(0).charAt(1));
            int aHua1 = Integer.parseInt(NowShouPai.get(0).charAt(7) + "");
            int aHua2 = Integer.parseInt(NowShouPai.get(1).charAt(7) + "");

            if (a > b) {
                pass = 0;
                return true;
            } else if (a == b && (aHua1 == 4 || aHua2 == 4)) {
                pass = 0;
                return true;
            } else {
                return false;
            }
        }

        public static boolean threeDuiBi() {
            int a = Integer.parseInt(NowShouPai.get(0).charAt(0) + "" + NowShouPai.get(0).charAt(1));
            int b = Integer.parseInt(UpShouPai.get(0).charAt(0) + "" + NowShouPai.get(0).charAt(1));
            if (a > b) {
                pass = 0;
                return true;
            } else {
                return false;
            }
        }

        public static boolean fourDuiBi() {
            int a = Integer.parseInt(NowShouPai.get(0).charAt(0) + "" + NowShouPai.get(0).charAt(1));
            int b = Integer.parseInt(UpShouPai.get(0).charAt(0) + "" + NowShouPai.get(0).charAt(1));
            if (a > b) {
                pass = 0;
                return true;
            } else {
                return false;
            }
        }

        public static boolean fireDuiBi() {
            String jieShouFire = FirePaiXu();
            int la = Integer.parseInt(NowShouPai.get(4).charAt(0) + "" + NowShouPai.get(4).charAt(1));
            int lb = Integer.parseInt(UpShouPai.get(4).charAt(0) + "" + NowShouPai.get(4).charAt(1));
            int za = Integer.parseInt(UpShouPai.get(2).charAt(0) + "" + NowShouPai.get(2).charAt(1));
            int zb = Integer.parseInt(UpShouPai.get(2).charAt(0) + "" + NowShouPai.get(2).charAt(1));
            int aHua = Integer.parseInt(NowShouPai.get(0).charAt(7) + "");
            int bHua = Integer.parseInt(UpShouPai.get(0).charAt(7) + "");

            if (NowShouPaiXing > UpYiShouPaiXing) {
                return true;
            } else if (NowShouPaiXing == UpYiShouPaiXing) {
                switch (NowShouPaiXing) {
                    case 9:
                        if (aHua > bHua) {
                            pass = 0;
                            return true;
                        } else {
                            return false;
                        }
                    case 8:
                        if (za > zb) {
                            pass = 0;
                            return true;
                        } else {
                            return false;
                        }
                    case 7:
                        if (za > zb) {
                            pass = 0;
                            return true;
                        } else {
                            return false;
                        }
                    case 6:
                        if (aHua > bHua) {
                            pass = 0;
                            return true;
                        } else {
                            return false;
                        }
                    case 5:
                        if (la > lb) {
                            pass = 0;
                            return true;
                        } else {
                            return false;
                        }
                    default:
                        return false;
                }
            } else {
                return false;
            }
        }
        //======================================
    }

    public void MH4GuiZe(String who) {

        switch (NowShouPai.size()) {
            case 1:

                if (NowShouPai.contains(meihua)) {
                    xiaYiJia(who);
                } else {
                    FanHui(who,whoCheck(who));
                }
                break;
            case 2:

                if (NowShouPai.contains(meihua)) {
                    if (guize.gzTwo()) {
                        xiaYiJia(who);
                    } else {
                        FanHui(who,whoCheck(who));
                    }
                } else {
                    FanHui(who,whoCheck(who));
                }
                break;
            case 3:

                if (NowShouPai.contains(meihua)) {
                    if (guize.gzThree()) {
                        xiaYiJia(who);
                    } else {
                        FanHui(who,whoCheck(who));
                    }
                } else {
                    FanHui(who,whoCheck(who));
                }
                break;
            case 4:

                if (NowShouPai.contains(meihua)) {
                    if (guize.gzFour()) {
                        xiaYiJia(who);
                    } else {
                        FanHui(who,whoCheck(who));
                    }
                } else {
                    FanHui(who,whoCheck(who));
                }
                break;
            case 5:

                if (NowShouPai.contains(meihua)) {
                    if (guize.gzFire()) {
                        UpYiShouPaiXing = NowShouPaiXing;
                        xiaYiJia(who);
                    }
                } else {
                    FanHui(who,whoCheck(who));
                }
                break;
        }


    }//梅花4出牌规则

    public void AllPassGuiZe(String who) {

        switch (NowShouPai.size()) {
            case 1:

                xiaYiJia(who);
                break;
            case 2:

                if (guize.gzTwo()) {
                    xiaYiJia(who);
                } else {
                    FanHui(who,whoCheck(who));
                }
                break;
            case 3:

                if (guize.gzThree()) {
                    xiaYiJia(who);
                } else {
                    FanHui(who,whoCheck(who));
                }
                break;
            case 4:

                if (guize.gzFour()) {
                    xiaYiJia(who);
                } else {
                    FanHui(who,whoCheck(who));
                }
                break;
            case 5:
                if (guize.gzFire()) {
                    UpYiShouPaiXing = NowShouPaiXing;
                    xiaYiJia(who);
                } else {
                    FanHui(who,whoCheck(who));
                }
                break;
        }
    }//所有人PASS出牌规则

    public void PuTongGuiZe(String who) {
        System.out.println("执行判断");
        switch (UpShouPai.size()) {
            case 1:
                if (guize.danDuiBi()) {
                    xiaYiJia(who);
                } else {
                    FanHui(who,whoCheck(who));
                }
                break;
            case 2:

                if (guize.twoDuiBi()) {
                    xiaYiJia(who);
                } else {
                    FanHui(who,whoCheck(who));
                }
                break;
            case 3:

                if (guize.threeDuiBi()) {
                    xiaYiJia(who);
                } else {
                    FanHui(who,whoCheck(who));
                }
                break;
            case 4:

                if (guize.fourDuiBi()) {
                    xiaYiJia(who);
                } else {
                    FanHui(who,whoCheck(who));
                }
                break;
            case 5:
                if (guize.fireDuiBi()) {
                    UpYiShouPaiXing = NowShouPaiXing;
                    xiaYiJia(who);
                } else {
                    FanHui(who,whoCheck(who));
                }

                break;
        }
    }//常规出牌规则

    public void GenHuan() {
        if (NowShouPai.size() != 0 && pass == 0) {//当上一手牌有牌时执行
            UpShouPai.clear();//先删除上一手牌
            UpShouPai.addAll(NowShouPai);//更换上一手牌
            NowShouPai.clear();//现一手牌添加到上一手牌完毕，删除。
            System.out.println("更换成功");
            pass = 0;
            System.out.println("上一手::"+UpShouPai);
            System.out.println("现一手::"+NowShouPai);
        } else {//相当于上一手是pass

        }


    }//更换上一手牌；并且添加HuiHeShuxunhuan+1，记录一次操作成功，用来显示回合数

    public void FanHui(String who,ArrayList<String> who1) {
        NowShouPai.clear();
        Play.clear();
        switch (who) {
            case "play":
                Play.addAll(beifen);
                beifen.clear();
                WanJia();
                break;
            case "AI1":
                AI1c();
                break;
            case "AI2":
                AI2c();
                break;
            default:
                AI3c();
                break;
        }
    }//错误出牌返回当前操作人员，并先清空现一手牌

    public void xiaYiJia(String who) {
        PanDuanDJShengChu();
        PanDuanPTShengChu();
        beifen.clear();
        GenHuan();
        HuiHeShuxunhuan++;
        switch (who) {
            case "play":
                System.out.println("轮到ai1");
                AI1c();
                break;
            case "AI1":
                System.out.println("轮到ai2");
                AI2c();
                break;
            case "AI2":
                System.out.println("轮到ai3");
                AI3c();
                break;
            case "AI3":
                System.out.println("轮到play");
                WanJia();
                break;
        }
    }//成功出牌，跳转下一位玩家
    //=============================================================================

    //操作========================================================================

    //玩家操作------------------------------------------------------------
    public void WanJia() {
        System.out.println("AI1手牌："+AI1+AI1.size());
        System.out.println("AI2手牌:"+AI2+AI2.size());
        System.out.println("AI3手牌:"+AI3+AI3.size());
        beifen.addAll(Play);//备份玩家手牌
        PassCheck();//先检查是否上一手最大
        String who = "play";
        boolean pd = playJA == 1 && playJ3 == 1;
        boolean pJ3JA = playJA == 1 || playJ3 == 1;
        System.out.println("当前回合" + HuiHeShuxunhuan);
        if (pd && HuiHeShuxunhuan == 0) {
            System.out.println("您的手牌");
            ShowPlayPai(Play);//展示玩家手牌
            System.out.println("这把你担鸡,是否继续,退出0,继续1");
            Scanner sc = new Scanner(System.in);
            if (sc.nextInt() == 0) {
                System.out.println("游戏结束平民胜利");
                System.exit(0);
            }
        }

        if (Play.size() == 0) {
            if (pd) {
                System.out.println("玩家担鸡胜出");
                System.exit(0);
            } else {
                if (pJ3JA) {
                    DJzhuP++;
                } else {
                    PMzhuP++;
                }
            }
            pass++;
            xiaYiJia(who);
        } else {
            System.out.println("您的手牌");
            ShowPlayPai(Play);//展示玩家手牌
            Scanner sc = new Scanner(System.in);
            if (playMH4 == 1 && HuiHeShuxunhuan == 0) {
                System.out.println("您持有梅花4，所以您先出牌,出牌必须包含梅花4");
                System.out.println("选着你要出的牌型 1.单张 2.对子 3.三条脱裤子 4.四条脱裤子 5.顺子/同花/3+2/4+1/同花顺 ");

                int choose = sc.nextInt();
                WanJiaChuPai(choose, who);//选择出牌阶段
                PaiXu(NowShouPai);//排序现一手牌
                MH4GuiZe(who);//判断出的牌合不合规则，能不能出,梅花4先出规则
            } else if (UpShouPai.size() == 0) {
                System.out.println("您的上一手牌无人能敌,你可以任意选择合规则的牌型出");
                System.out.println("选着你要出的牌型 1.单张 2.对子 3.三条脱裤子 4.四条脱裤子 5.顺子/同花/3+2/4+1/同花顺");

                int choose = sc.nextInt();
                if (choose > 5 || choose < 1) {
                    System.out.println("输入错误");
                    FanHui(who,whoCheck(who));
                }
                WanJiaChuPai(choose, who);//选择出牌阶段
                PaiXu(NowShouPai);//排序现一手牌
                AllPassGuiZe(who);//判断出的牌合不合规则，能不能出,所以玩家pass之后出的规则
            } else {
                ShowUpYiShouPai();//先展示上一手牌
                System.out.println("输入6不出");
                int choose = sc.nextInt();
                if (choose != UpShouPai.size()) {
                    if (choose == 6) {
                        pass++;
                        System.out.println("玩家pass,pass数：" + pass);
                        xiaYiJia(who);
                    }
                    FanHui(who,whoCheck(who));
                }
                WanJiaChuPai(choose, who);//选择出牌阶段
                PaiXu(NowShouPai);//排序现一手牌
                System.out.println("上一手:"+UpShouPai);
                System.out.println("先一手:"+NowShouPai);
                PuTongGuiZe(who);


            }
        }

    }

    public void WanJiaChuPai(int x, String who) {

        Scanner sc = new Scanner(System.in);
        StringBuilder s = new StringBuilder();
        if (x <= 5 && x >= 1) {
            for (int i = 0; i < x; i++) {
                System.out.println("选择你要出第" + (i + 1) + "牌");
                int xz = sc.nextInt();
                if (PlayCheck(xz)) {//正确就添加进现一手牌中
                    NowShouPaiAdd(xz, whoCheck(who));
                    ShowPlayPai(Play);//展示玩家手牌
                } else {
                    FanHui(who,whoCheck(who));
                }
            }

        } else if (x == 6) {
            //不出操作
            pass++;
            xiaYiJia(who);
        } else {
            FanHui(who,whoCheck(who));
        }
    }

    public boolean PlayCheck(int x) {
        return x <= Play.size() && x >= 1;
    }//判断玩家选择的牌是否存在

    public void NowShouPaiAdd(int x, ArrayList<String> who) {
        System.out.println(x-1);
        NowShouPai.add(who.get((x-1)));
        System.out.println("添加成功");
        who.remove(x - 1);

    }//添加到现一手牌中

    public ArrayList<String> whoCheck(String who) {
        switch (who) {
            case "play":
                return Play;
            case "AI1":
                return AI1;
            case "AI2":
                return AI2;
            default:
                return AI3;
        }
    }//判断传入的who，返回对应的玩家牌集合

    public void PassCheck() {
        if (pass == 3) {
            pass = 0;
            UpShouPai.clear();
        }
    }//检查pass是否为3，是就是上一手全场最大
    //------------------------------------------------------------------

    //ai操作------------------------------------------------------------
    public void AI1c() {
        System.out.println("当前pass数："+pass);
        boolean ai1d = AI1.size() == 0 && ai1JA == 1 && ai1J3 == 1;
        boolean ai1J3JA = ai1JA == 1 || ai1J3 == 1;
        Random r = new Random();
        String who = "AI1";
        if (ai1d && HuiHeShuxunhuan == 0) {
            if (r.nextInt(2) == 0) {
                System.out.println("ai1担鸡放弃游戏");
                System.out.println("游戏结束平民胜利");
                System.exit(0);
            }
        }

        if (AI1.size() == 0) {
            if (ai1d) {
                System.out.println("ai1担鸡胜出");
                System.exit(0);
            } else {
                if (ai1J3JA) {
                    DJzhuAI1++;
                } else {
                    PMzhuAI1++;
                }
            }
            if(pass == 3){
                pass = 0;
                UpShouPai.clear();
            }
            pass++;

            xiaYiJia(who);
        } else {
            PassCheck();//先检查是否上一手最大
            AIKaiShi(who);
        }

    }

    public void AI2c() {
        System.out.println("当前pass数："+pass);
        boolean ai2d = AI2.size() == 0 && ai2JA == 1 && ai2J3 == 1;
        boolean ai2J3JA = ai2JA == 1 || ai2J3 == 1;
        Random r = new Random();
        String who = "AI2";
        if (ai2d && HuiHeShuxunhuan == 0) {
            if (r.nextInt(2) == 0) {
                System.out.println("ai2担鸡放弃游戏");
                System.out.println("游戏结束平民胜利");
                System.exit(0);
            }
        }
        if (AI2.size() == 0) {
            if (ai2d) {
                System.out.println("ai2担鸡胜出");
                System.exit(0);
            } else {
                if (ai2J3JA) {
                    DJzhuAI2++;
                } else {
                    PMzhuAI2++;
                }
            }

            pass++;
            if(pass == 3){
                pass = 0;
                UpShouPai.clear();
            }
            xiaYiJia(who);
        } else {
            PassCheck();//先检查是否上一手最大
            AIKaiShi(who);
        }

    }

    public void AI3c() {
        System.out.println("当前pass数："+pass);
        boolean ai3d = AI3.size() == 0 && ai3JA == 1 && ai3J3 == 1;
        boolean ai3J3JA = ai3JA == 1 || ai3J3 == 1;
        Random r = new Random();
        String who = "AI3";
        System.out.println("AI3开始");
        if (ai3d && HuiHeShuxunhuan == 0) {
            if (r.nextInt(2) == 0) {
                System.out.println("ai3担鸡放弃游戏");
                System.out.println("游戏结束平民胜利");
                System.exit(0);
            }
        }
        if (AI3.size() == 0) {
            if (ai3d) {
                System.out.println("ai3担鸡胜出");
                System.exit(0);
            } else {
                if (ai3J3JA) {
                    DJzhuAI3++;
                } else {
                    PMzhuAI3++;
                }
            }
            pass++;
            xiaYiJia(who);
        } else {
            PassCheck();//先检查是否上一手最大
            AIKaiShi(who);
        }

    }

    public void AIKaiShi(String who) {

        if (ai1MH4 == 1 && HuiHeShuxunhuan == 0 || ai2MH4 == 1 && HuiHeShuxunhuan == 0 || ai3MH4 == 1 && HuiHeShuxunhuan == 0) {
            aiMH4(who, whoCheck(who));
        } else if (UpShouPai.size() == 0) {
            aiAllPass(who, whoCheck(who));
        } else {
            aiPuTong(who, whoCheck(who));
        }
    }

    public void aiMH4(String who, ArrayList<String> who1) {
        System.out.println(who+"执行梅花4规则操作");
        pass = 0 ;
        NowShouPai.add(0, who1.get(0));
        who1.remove(0);
        System.out.println(who + NowShouPai);
        xiaYiJia(who);
    }

    public void aiAllPass(String who, ArrayList<String> who1) {
        System.out.println(who+"执行allpass规则操作");

        pass = 0 ;
        NowShouPai.add(0, who1.get(0));
        who1.remove(0);
        System.out.println(who + NowShouPai);
        xiaYiJia(who);
    }

    public void aiPuTong(String who, ArrayList<String> who1) {
        System.out.println(who+"执行普通出牌规则操作");
        Random r = new Random();
        int a = Integer.parseInt(who1.get(who1.size() - 1).charAt(9) + "" + who1.get(who1.size() - 1).charAt(10));
        int b = Integer.parseInt(UpShouPai.get(0).charAt(9) + "" + UpShouPai.get(0).charAt(10));
        int rand = r.nextInt(2);
        System.out.println("现在是" + who + "出牌");
        System.out.println("上一手"+UpShouPai);
        if (rand == 0) {
            pass++;
            System.out.println(who + "pass,pass数：" + pass);
            xiaYiJia(who);
        } else {
            if (UpShouPai.size() == 1 && a > b) {
                pass = 0 ;
                NowShouPai.clear();
                NowShouPai.add(0, who1.get(who1.size() - 1));
                who1.remove(who1.size() - 1);
                System.out.println(who + NowShouPai);
                System.out.println("上一手"+UpShouPai);
                xiaYiJia(who);
            } else {
                pass++;
                System.out.println(who + "pass,pass数：" + pass);
                xiaYiJia(who);
            }
        }
    }

    static class AITongYiCaZuo {

    }
    //------------------------------------------------------------------

    //============================================================================
    public static void main(String[] args) {
        Fu fu = new Fu();
        fu.KaiShi();
    }
}
